(function() {
    const collapse = {};
    collapse.install = (Vue, options = {}) => {
        Vue.directive('collapse', {
            inserted(el, binding) {
                const COLLAPSED = '展开';
                const UNCOLLAPSED = '收起';
                const UNCOLLAPSED_HEIGHT = 0;
                // code start 2017-12-11 by cuihd
                const paddTop = 30; // 查询条件和form表单中间加一个30px的空间
                let newHeight = el.offsetHeight + paddTop; 
                let collapsedHeight = newHeight + 'px'; 
                // let collapsedHeight =  el.offsetHeight + 'px'; 
                // code end
                let trigger = document.createElement('span');
                let isCollapsed = true;
                let collapseText = COLLAPSED;
                let height = UNCOLLAPSED_HEIGHT;
                let _height = 0;
                if (typeof binding.value == 'boolean') {
                    isCollapsed = binding.value;
                }
                el.parentNode.appendChild(trigger);
                el.style.transition = 'height .3s';
                el.style.overflow = 'hidden';
                if (!isCollapsed) {
                    collapseText = UNCOLLAPSED;
                    height = collapsedHeight;
                    el.style.paddingTop = '0'; // 去除padding-top的值 2017-12-11 by cuihd
                }
                el.style.height = height;
                trigger.className = 'collapse-trigger';
                trigger.innerHTML = collapseText;
                trigger.style.position = 'absolute';
                trigger.style.top = '10px'; // 2017-12-11 by cuihd
                trigger.style.right = '20px'; // 2017-12-11 by cuihd
                trigger.style.color = '#ff5e2c';
                trigger.style.fontSize = '14px';
                trigger.style.cursor = 'pointer';
                trigger.addEventListener('click', e => {
                    if (isCollapsed) {
                        height = UNCOLLAPSED_HEIGHT;
                        collapseText = UNCOLLAPSED;
                        if (!(parseInt(collapsedHeight)-30)) { // 将已经加上的30px去掉，进行bool判断 2017-12-11 by cuihd
                            el.style.height = 'auto';
                            // code start 2017-12-11 by cuihd
                            newHeight = el.offsetHeight + paddTop; 
                            collapsedHeight = newHeight + 'px'; 
                            // collapsedHeight = el.offsetHeight + 'px'; 
                            // code end
                        }
                        el.style.paddingTop = paddTop + 'px'; // 2017-12-11 by cuihd
                    } else {
                        el.style.paddingTop = '0'; // 2017-12-11 by cuihd
                        height = collapsedHeight;
                        collapseText = COLLAPSED;
                        el.style.transition = 'all .3s'; // 2017-12-11 by cuihd
                    }
                    isCollapsed = !isCollapsed;
                    trigger.innerHTML = collapseText;
                    el.style.height = height;
                    requestAnimationFrame(() => {
                        el.style.height = isCollapsed ? UNCOLLAPSED_HEIGHT : collapsedHeight;
                    });
                });
            }
        });
    };
    if (typeof exports == 'object') {
        module.exports = collapse
    } else if (typeof define == 'function' && define.amd) {
        define([], () => collapse)
    } else if (window.Vue) {
        window.collapse = collapse;
        Vue.use(collapse)
    }
}());